import sys
import re
def print_syntax(syntax,num):
	f1=syntax.strip('"').replace('{','{ ').replace('}',' }')
	if f1.startswith('['):
		f2=f1.split(' ',1)
		c=f2[0][len(f2[0])-2]
		b=f2[1]
	else:
		c=''
		b=f1
	
	b1=b.split(' ')
	b2=[]
	b3=[]
	for bb in b1:
		bbb=bb.strip(' ')
		if len(bbb)==0:
			continue
		
		if bbb=='op':
			b2.append('%s')
			b3.append(bbb)
		elif bbb.startswith('v') and not bbb.startswith('vtaboff'):
			s='v%s'
			if bbb.endswith(','):
				b2.append(s+',')
				b3.append(bbb[1:-1])
			else:
				b3.append(bbb[1:])				
				b2.append(s)	
		else:
			a=bbb.find('@')
			if a!=-1:
				s=bbb.replace('AAAA','%s').replace('BBBB','%s')	
				b2.append(s)
				b3.append(bbb[a+1:])
			else:
				a=bbb.find('+')
				if a!=-1:
					s=bbb.replace('AAAA','%s').replace('BBBB','%s')	
					b2.append(s)
					b3.append(bbb[a+1:])
				else:				
					b2.append(bbb)									
	if c!='':
		if num==0:
			print('\t\tif (p_insn->A==%s){'%(c))
		else:
			print('\t\telse if (p_insn->A==%s){'%(c))	

	fmt=' '.join(b2)
	
	args=''
	n=len(b3)	
	for b4 in b3:
		if b4=='NNNN':
			b5='CCCC+p_insn->AA-1' 
		elif b4=='BBBBBBBB':          
			b5='BBBBlo,p_insn->BBBBhi'
		elif b4=='BBBBBBBBBBBBBBBB':          
			b5='BBBBlo,p_insn->BBBBmilo,p_insn->BBBBmihi,p_insn->BBBBhi'	
		elif b4=='BBBB000000000000' or b4=='BBBB0000':          
			b5='BBBB'					
		elif b4=='AAAAAAAA':          
			b5='AAAAlo,p_insn->AAAAhi'
		else:
			b5=b4
		args=args+ 'p_insn->'+b5
		if n>1:
			args=args+','

		n=n-1
	if c!='':		
		if len(args)>0:
			print('\t\t\tprintf("%s",%s);'%(fmt,args))
		else:
			print('\t\t\tprintf("%s");'%(fmt))
		print('\t\t}')
	else:
		if len(args)>0:	
			print('\t\tprintf("%s",%s);'%(fmt,args))
		else:
			print('\t\t\tprintf("%s");'%(fmt))

			
if len(sys.argv)<2:
     print("usage:")
     exit(0)

myfile = open(sys.argv[1], "r")
bytes=[]
ll=[]
l=[]
for line in (myfile):
	if len(line)==0:
		continue
	if line.startswith('#'):
		continue
	if line.startswith('\t\t'):
		c=2				
	elif line.startswith('\t'):
		c=1
	else:
		c=0
			
	s=line.strip(' \t\n')
	if len(s)==0:
		continue
	s1=s.split('\t')	
	if c==0:
		fmt=''
		syntax=[]		
		n=len(s1)
		i=0
		bytes=[]
		while i<n:
			if s1[i][0].isdigit():
				break
			bytes=bytes+s1[i].split(' ')
			i=i+1
		if i<n:
			fmt=s1[i]
			if n>i+1:
				syntax=[s1[i+1]]

		l=[bytes,fmt,syntax]
		ll.append(l)
	elif c==2:
		l[2].append(s1[0])	
	else:
		n=len(s1)	
		fmt=s1[0]
		if n>1:
			syntax=[s1[1]]
		else:
			syntax=[]

		l=[bytes,fmt,syntax]
		ll.append(l)		

print('''
#include <stdio.h>
#include "dalvik.h"
#include "dalvik-opcode.h"

extern unsigned insn_len_of_fmt[]={
''')
n=len(ll)
for x in ll:
	if n>1:
		print('%s,//fmt_%s,'%(x[1][0],x[1]))
	else:
		print('%s,//fmt_%s,'%(x[1][0],x[1]))	
	n=n-1

print('''
};

void print_fmt(PINSN_INFO p_insn_info)
{
''')




print('\tswitch(p_insn_info->fmt)\n\t{')
for x in ll:
	print('\tcase fmt_%s:\n\t{'%(x[1]))	
	print('\t\tINSN_INFMT_%s * p_insn=(INSN_INFMT_%s *)p_insn_info->code;'%(x[1],x[1]))	
	i=0
	for syntax in x[2]:
		print_syntax(syntax,i)
		i=i+1
	print('\t}')
	print('\tbreak;')	
print('''
   }
}
''')